package main

import (
	"autoTestNew/pkg/config"
	"autoTestNew/pkg/dashboard/apiServer"
	autotest "autoTestNew/pkg/dashboard/autoTest"
	"autoTestNew/pkg/log"
	"autoTestNew/pkg/mongo"
	"context"
	"flag"
	fxlogr "github.com/chaos-mesh/fx-logr"
	"go.uber.org/fx"
	stdlog "log"
	"os"
	ctrl "sigs.k8s.io/controller-runtime"
)

func main() {
	var printVersion bool

	flag.BoolVar(&printVersion, "version", false, "print version information and exit")
	flag.Parse()

	if printVersion {
		os.Exit(0)
	}

	rootLogger, err := log.NewDefaultZapLogger()
	if err != nil {
		stdlog.Fatal("failed to create root logger", err)
	}

	ctrl.SetLogger(rootLogger)
	mainLog := rootLogger.WithName("main")
	fxLogger := rootLogger.WithName("fx")
	dashboardConfig, err := config.GetChaosDashboardEnv()
	persistTTLConfigParsed, err := dashboardConfig.PersistTTL.Parse()
	if err != nil {
		mainLog.Error(err, "invalid PersistTTLConfig")
		os.Exit(1)
	}

	controllerRuntimeSignalHandlerContext := ctrl.SetupSignalHandler()
	app := fx.New(
		fx.WithLogger(fxlogr.WithLogr(&fxLogger)),
		fx.Supply(rootLogger),
		fx.Provide(
			func() (context.Context, *config.ChaosDashboardConfig, *config.TTLConfig, *config.MongoDBConfig) {
				return controllerRuntimeSignalHandlerContext, dashboardConfig, persistTTLConfigParsed, dashboardConfig.MongoDB
			},
			mongo.Bootstrap,
			autotest.Bootstrap,
		),
		//store.Module, stroe的作用是什么？
		apiServer.Module,
		mongo.Module,
	)
	app.Run()
}
